/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

export default class TreeNode{
    val: number
    left: TreeNode | null
    right: TreeNode | null
    constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
        this.val = (val === undefined ? 0 : val)
        this.left = (left === undefined ? null : left)
        this.right = (right === undefined ? null : right)
    }
}

function searchBST(root: TreeNode | null, val: number): TreeNode | null {
    if(!root) return null;
    if(val === root.val) return root;
    return searchBST(val<root.val?root.left:root.right, val)
};
const TreeNode2 = new TreeNode(4,new TreeNode(2,new TreeNode(1),new TreeNode(3,new TreeNode(),new TreeNode())))  
const result = searchBST(TreeNode2,2)
console.log(result)
